import 'package:my_flutter_core/my_flutter_core.dart';

class AppReleaseLogService {
  final String _url = '/app-release-log';
  final HttpRequestService _httpRequestService = HttpRequestService();

  /// Retrieves the [AppInfo] detail info by version.
  Future<AppInfo?> queryByAppVersion(String appVersion) async {
    // Gets the response of the http get request.
    final response = await _httpRequestService
        .get('$_url/query-by-app-version', params: {'appVersion': appVersion});

    // Parses the data from [Map<String, dynamic>] to [AppInfo].
    return response.payload != null &&
            (response.payload as Map<String, dynamic>).isNotEmpty
        ? AppInfo.fromJson(response.payload)
        : null;
  }

  /// Checks if has the updated version by releaseDate.
  Future<CheckAppUpdateResult> checkUpdate(String appVersion) async {
    // Gets the response of the http get request.
    final response = await _httpRequestService
        .get('$_url/query-latest-version', params: {'appVersion': appVersion});

    // Parses the data from [Map<String, dynamic>] to [CheckUpdateResult].
    return CheckAppUpdateResult.fromJson(response.payload);
  }

  /// Retrieves current user account group [IncomeExpenseDetail] list from backend service.
  Future<List<AppInfo>> listChangeLog(int pageSize, int pageIndex) async {
    // Gets the response of the http get request.
    // Formats the date parameter to clear the time value.
    final response =
        await _httpRequestService.get('$_url/list-change-log', params: {
      'pageSize': pageSize,
      'pageIndex': pageIndex,
    });

    // Parses the data from [Map<String, dynamic>] to [AppInfo].
    return (response.payload as List<dynamic>)
        .map((p) => AppInfo.fromJson(p))
        .toList();
  }
}
